#ifndef __REDIS_UTIL__
#define __REDIS_UTIL__

#include <sw/redis++/redis++.h>
#include "config.hpp"

namespace cloud
{
    class RedisUtil
    {
    private:
        sw::redis::Redis *redis;
        static std::mutex _mutex;
        static RedisUtil *_instance;

    private:
        RedisUtil()
        {
            redis = new sw::redis::Redis(Config::GetInstance()->GetRedisURL());
        }

    public:
        static RedisUtil *GetInstance()
        {
            if (_instance == nullptr) // 双重判断减少加锁解锁次数,提高性能
            {
                _mutex.lock();
                if (_instance == nullptr)
                    _instance = new RedisUtil();
                _mutex.unlock();
            }

            return _instance;
        }

        bool Get(const std::string &key,std::string& value)
        {
            auto tmp=redis->get(key);
            if(tmp)
            {
                value=tmp.value();
                return true;
            }
            return false;
        }
        void Set(const std::string &key, const std::string &value)
        {
            redis->set(key, value);
        }
    };
    RedisUtil *RedisUtil::_instance = nullptr;
    std::mutex RedisUtil::_mutex;
}

#endif //__REDIS_UTIL__